package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.itheima.dubbo.api.FriendApi;
import com.itheima.dubbo.api.UserApi;
import com.itheima.dubbo.api.UserInfoApi;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.domain.Friend;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserThreadLocal;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author Pan Rundong
 * @Date 2023-03-04 16:33
 * @Desc 通信Service
 */
@Service
public class MessagesService {
    @DubboReference(version = "1.0.0")
    private UserApi userApi;
    @DubboReference(version = "1.0.0")
    private UserInfoApi userInfoApi;
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @DubboReference
    private FriendApi friendApi;

    /**
     * 根据环信Id查询User信息
     *
     * @param huanxinId
     * @return com.tanhua.model.vo.UserInfoVo
     * @Author Pan Rundong
     */
    public UserInfoVo userInfo(String huanxinId) {
        //1.根据环信Id查询User信息
        User user = userApi.findByHuanXinId(huanxinId);
        //2.根据查询到的User信息查询UserInfo信息
        UserInfo userInfo = userInfoApi.findById(user.getId());

        //3.判断UserInfo是否存在
        if (userInfo == null) {
            //不存在，返回空
            return new UserInfoVo();
        } else {
            //存在，封装Vo并返回
            UserInfoVo vo = new UserInfoVo();
            BeanUtils.copyProperties(userInfo,vo); //copy同名同类型的属性
            if(userInfo.getAge() != 0) {
                StringBuilder sb = new StringBuilder("");
                vo.setAge(sb.append(userInfo.getAge()).toString());
            }
            return vo;
        }
    }

    /**
     * 添加联系人
     *
     * @param friendId
     * @return void
     * @Author Pan Rundong
     */
    public void contacts(Long friendId) {
        //1.获取当前用户Id
        Long currentUserId = UserThreadLocal.getUserId();

        //2.调用Api注册环信好友
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + currentUserId,
                Constants.HX_USER_PREFIX + friendId);

        //3.判断是否注册成功
        if (!aBoolean) {
            return;
        }

        //4.注册成功，保存
        friendApi.save(currentUserId, friendId);
    }

    /**
     * 移除联系人
     *
     * @param friendId
     * @return void
     * @Author Pan Rundong
     */
    public void removeContacts(Long friendId) {
        //1.获取当前用户Id
        Long currentUserId = UserThreadLocal.getUserId();

        //2.调用Api注册环信好友
        Boolean aBoolean = huanXinTemplate.deleteContact(Constants.HX_USER_PREFIX + currentUserId,
                Constants.HX_USER_PREFIX + friendId);

        //3.判断是否删除
        if (!aBoolean) {
            return;
        }

        //4.删除成功，在表中删除
        friendApi.delete(currentUserId, friendId);
    }

    /**
     * 联系人列表
     *
     * @param page
     * @param pagesize
     * @param keyword
     * @return com.tanhua.model.vo.ContactVo
     * @Author Pan Rundong
     */
    public PageResult getContacts(Integer page, Integer pagesize, String keyword) {
        //1.调用api查询信息用户好友信息
        Long userId = UserThreadLocal.getUserId();
        List<Friend> friendList = friendApi.findById(page, pagesize, userId);

        //2.获取好友Id
        List<Long> ids = CollUtil.getFieldValues(friendList, "friendId", Long.class);

        //3.调用api获取对应条件userInfo
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(keyword);
        Map<Long, UserInfo> map = userInfoApi.findByIds(ids, userInfo);

        //4.封装Vo
        List<ContactVo> voList = ids.stream().map((id) -> {
            UserInfo userInfoM = map.get(id);
            ContactVo vo = ContactVo.init(userInfoM);
            return vo;
        }).collect(Collectors.toList());

        return new PageResult(page, pagesize, voList.size(), voList);
    }
}
